/**
 * 针对 https://github.com/xaboy/form-create-designer 封装的工具类
 */

import { getToken } from '/@/utils/auth';
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();

// 编码表单 Conf
export const encodeConf = (designerRef: object) => {
  // @ts-ignore
  return JSON.stringify(designerRef.value.getOption());
};

// 编码表单 Fields
export const encodeFields = (designerRef: object) => {
  // @ts-ignore
  const rule = designerRef.value.getRule();
  const fields: string[] = [];
  rule.forEach((item) => {
    if (item.effect?.fetch?.action) {
      Reflect.set(item.effect.fetch, 'headers', { Authorization: 'Bearer ' + getToken() });
    }
    if (item.props?.action) {
      Reflect.set(item.props, 'headers', { Authorization: 'Bearer ' + getToken() });
    }
    console.log(JSON.stringify(item));
    fields.push(JSON.stringify(item));
  });
  return fields;
};
// 解码表单 Fields
export const decodeFields = (fields: string[]) => {
  const rule: object[] = [];
  fields.forEach((item) => {
    const obj = JSON.parse(item);
    if (obj.type === 'upload') {
      obj.props.listType = 'text';
      obj.props.onSuccess = function (res, file) {
        file.url = res.data;
      };
      obj.props.onPreview = function (file) {
        window.open(file.url);
      };
    }
    if (obj.effect?.fetch?.action) {
      obj.effect.fetch.headers = { Authorization: 'Bearer ' + getToken() };
    }
    if (obj.props?.action) {
      obj.props.headers = { Authorization: 'Bearer ' + getToken() };
    }
    if (obj.field === 'userId') {
      obj.value = userStore.getUserInfo.user.id;
    }
    if (obj.field === 'deptId') {
      obj.value = userStore.getUserInfo.user.deptId;
    }

    rule.push(obj);
    // // console.log(JSON.parse(item));
    // // if (JSON.parse(item).field === 'F4np5znfhmphn') {
    // //   console.log('2222');
    // //   const obj = {
    // //     type: 'input',
    // //     field: 'F4np5znfhmphn',
    // //     title: '领用人',
    // //     control: [
    // //       {
    // //         handle(val) {
    // //           return val === '4';
    // //         },
    // //         rule: [
    // //           {
    // //             type: 'input',
    // //             field: 'info',
    // //             title: '差评原因',
    // //             value: 'default info',
    // //           },
    // //         ],
    // //       },
    // //     ],
    // //   };

    // //   rule.push(obj);
    // // } else {
    // rule.push(JSON.parse(item));
    // // }
  });
  return rule;
};

// 设置表单的 Conf 和 Fields
export const setConfAndFields = (designerRef: object, conf: string, fields: string) => {
  // @ts-ignore
  designerRef.value.setOption(JSON.parse(conf));
  // @ts-ignore
  designerRef.value.setRule(decodeFields(fields));
};

// 设置表单的 Conf 和 Fields
export const setConfAndFields2 = (
  detailPreview: object,
  conf: string,
  fields: string,
  value?: object,
) => {
  // @ts-ignore
  // console.log('--------------', detailPreview, conf, fields, value);
  // console.log('--------------', fields);
  // console.log(JSON.parse(conf));
  // console.log(fields);
  detailPreview.value.option = JSON.parse(conf);
  // @ts-ignore
  detailPreview.value.rule = decodeFields(fields);
  if (value) {
    // @ts-ignore
    detailPreview.value.value = value;
  }
};
